package com.spc.car.web.controller.sys;

import com.spc.car.common.utils.DateUtils;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.domain.sys.po.SysCalendarListPO;
import com.spc.car.data.domain.sys.po.SysCalendarPO;
import com.spc.car.data.entity.sys.SysCalendar;
import com.spc.car.data.service.sys.SysCalendarService;
import com.spc.car.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * <p>
 * 日程表 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-03-27
 */
@Api(tags = "日历")
@Validated
@RestController
@RequestMapping("/calendar")
public class SysCalendarController extends BaseController {

    @Autowired
    private SysCalendarService sysCalendarService;

    @ApiOperation(value = "添加/更新日历" , notes = "添加/更新日历")
    @PostMapping("/saveOrUpdate")
    public  AjaxResult saveOrUpdate(@Valid @RequestBody SysCalendarPO sysCalendarPO){

        SysCalendar sysCalendar = new SysCalendar();
        BeanUtils.copyProperties(sysCalendarPO , sysCalendar);
        sysCalendar.setCreatorId(getPrintUser().getUserId());
        sysCalendar.setCreator(getPrintUser().getUserName());
        boolean result = sysCalendarService.saveOrUpdate(sysCalendar);
        return  result ? AjaxResult.success("添加/更新日历成功") : AjaxResult.error("添加/更新日历失败");

    }

    @ApiOperation(value = "删除日历" , notes = "删除日历")
    @PostMapping("/remove")
    public  AjaxResult remove(@NotNull Integer id){

        int flag = sysCalendarService.removeCalendar(id);
        return  flag != 0 ? AjaxResult.success("日历删除成功") : AjaxResult.error("日历删除失败");

    }


    @ApiOperation(value = "获取日历列表" , notes = "获取日历列表")
    @PostMapping("/listCalendar")
    public AjaxResult listCalendar(@Valid @RequestBody SysCalendarListPO sysCalendarListPO ){

        List<SysCalendar> calendarList = sysCalendarService.selectCalendarByDate(
                getPrintUser().getUserId(),
                DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD , sysCalendarListPO.getStartDate()),
                DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, sysCalendarListPO.getEndDate()));
        return  AjaxResult.success(calendarList);

    }

}

